package com.caj.id;


import redis.clients.jedis.Jedis;

import java.util.Calendar;
import java.util.Date;

public class Redis {

    private Jedis jedis = new Jedis("127.0.0.1", 6379);

    public String nextId(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(Calendar.YEAR);
        int day = calendar.get(Calendar.DAY_OF_YEAR);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //jedis是非线程安全的
        Long orderId = jedis.incr("orderId");

        return String.format("%1$04d", year) + String.format("%1$03d", day) + String.format("%1$02d", hour) +
                String.format("%1$05d", orderId);
    }
}
